//
//  HLLocationManager.m
//  YueYue
//
//  Created by hulu001 on 2023/5/17.
//

#import "HLLocationManager.h"
#import "LBXPermissionLocation.h"

@interface HLLocationManager () <CLLocationManagerDelegate>

@property(nonatomic,strong) CLLocationManager *locationManager;
@property(nonatomic,copy)void(^success)(CLLocation* currentLocation);
@property(nonatomic,copy)void(^fail)(NSError* error);

@end

@implementation HLLocationManager



+ (instancetype)sharedManager
{
    static HLLocationManager* _sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[HLLocationManager alloc] init];
    });
    
    return _sharedInstance;
}


//GPS
- (void)startGps:(void(^)(CLLocation* currentLocation))success fail:(void(^)(NSError* error))fail
{
    
    [self stopGps];
    
    self.success = success;
    self.fail = fail;
    
    [self startGps];
}

- (void)startGps
{
    
    if ( self.locationManager != nil ) {
        [self stopGps];
        return;
    }
    
    self.locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate=self;
    _locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
    
    [_locationManager requestAlwaysAuthorization];
    [_locationManager requestWhenInUseAuthorization];
    [_locationManager startUpdatingLocation];
    
}



- (void)stopGps
{
    if ( self.locationManager )
    {
        [_locationManager stopUpdatingLocation];
        self.locationManager = nil;
        
        self.success = nil;
        self.fail = nil;
    }
}


- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

    switch (status)

    {
        case kCLAuthorizationStatusNotDetermined:

            if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])

            {
                [_locationManager requestAlwaysAuthorization];
            }

            break;

        default:

            break;

    }
}


- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    //CLLocationCoordinate2D coordinate = [newLocation coordinate];
    
   //调用者负责停止GPS
   // [self stopGps];
    CLLocation *newLocation = locations.firstObject;
    CLLocationCoordinate2D coordinate = [newLocation coordinate];
    _latitude = coordinate.latitude;
    _longtitude = coordinate.longitude;
    
    if (_success)
    {
        _success(newLocation);
    }
}



////------------------位置反编码---5.0之后使用-----------------
//CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//[geocoder reverseGeocodeLocation:newLocation
//               completionHandler:^(NSArray *placemarks, NSError *error){
//
//                   for (CLPlacemark *place in placemarks) {
//                       UILabel *label = (UILabel *)[self.window viewWithTag:101];
//                       label.text = place.name;
//                       NSLog(@"name,%@",place.name);                       // 位置名
//                       //                           NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道
//                       //                           NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道
//                       //                           NSLog(@"locality,%@",place.locality);               // 市
//                       //                           NSLog(@"subLocality,%@",place.subLocality);         // 区
//                       //                           NSLog(@"country,%@",place.country);                 // 国家
//                   }
//
//               }];

/**
 @brief 转换GPS->城市名称
 @param currentLocation 当前坐标
 @param success         返回结果
 */
+ (void)converseGps:(CLLocation*) currentLocation
         success:(void (^)(CLPlacemark *placemark))success
               fail:(void(^)(NSError* error))fail
{
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             
             if (success) {
                 success(placemark);
             }
             
         }
         else
         {
             if (fail) {
                 fail(error);
             }
         }
     }];
}


- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"GPS error:%@",error);
    
    if (_fail) {
        _fail(error);
    }
}


@end
